package dao;

import bean.Sell;
import util.Dbconnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class SellDao {

    public static List<Sell> selvip() {

        List<Sell> list = new ArrayList<Sell>();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = Dbconnection.getcConnection();
            String sql = "select * from sell";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Sell sho = new Sell();
                sho.setOnum(rs.getInt("onum"));
                sho.setName(rs.getString("name"));
                sho.setGender(rs.getInt("gender"));
                sho.setAge(rs.getInt("age"));
                list.add(sho);
                System.out.println(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, ps, rs);
        }

        return list;
    }

    public static void del(String id) {
        Connection conn = null;
        Statement stat = null;
        int rs;
        String sql = "delete from Sell where onum='" + id + "'";
        try {
            conn = Dbconnection.getcConnection();
            stat = conn.createStatement();
            rs = stat.executeUpdate(sql);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, stat, null);
        }

    }

    public static void insert(int no, String name, int time, int address) {
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = Dbconnection.getcConnection();
            String sql = "insert into sell (onum,name,gender,age) value(?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, no);
            ps.setString(2, name);
            ps.setInt(3, time);
            ps.setInt(4, address);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, ps, null);
        }
    }

    public static Sell upda(int id) {

        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        String sql = "select * from sell where onum='" + id + "'";

        try {
            conn = Dbconnection.getcConnection();
            stat = conn.prepareStatement(sql);
            rs = stat.executeQuery(sql);
            while (rs.next()) {
                Sell shop = new Sell();
                shop.setOnum(rs.getInt("onum"));
                shop.setName(rs.getString("name"));
                shop.setGender(rs.getInt("gender"));
                shop.setAge(rs.getInt("age"));
                return shop;

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, stat, null);
        }
        return null;
    }

    public void upda1(Sell shop) {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean isok = false;
        try {
            conn = Dbconnection.getcConnection();
            String sql = "update Sell set name=?,gender=?,age=? where onum=?";
            System.out.println(sql);
            ps = conn.prepareStatement(sql);
            ps.setString(1, shop.getName());
            ps.setInt(2, shop.getGender());
            ps.setInt(3, shop.getAge());
            ps.setInt(4, shop.getOnum());
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbconnection.close(conn, ps, null);
        }
    }
}
